package com.zzstm.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzstm.eduservice.entity.EduChapter;
import com.zzstm.eduservice.entity.EduVideo;
import com.zzstm.eduservice.entity.chapter.ChapterVO;
import com.zzstm.eduservice.entity.chapter.VideoVO;
import com.zzstm.eduservice.entity.vo.CourseInfoVO;
import com.zzstm.eduservice.mapper.EduChapterMapper;
import com.zzstm.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzstm.eduservice.service.EduVideoService;
import com.zzstm.servicebase.ex.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-10-30
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    //课程大纲列表，根据课程id查询
    @Override
    public List<ChapterVO> getChapterVideoByCourseId(String courseId) {
        //1、根据课程id查课程里面的所有章节
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(chapterQueryWrapper);
        //2、根据课程id查课程里面的所有小节
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courseId);
        List<EduVideo> videoList = videoService.list(videoQueryWrapper);
        //3、遍历查询章节list集合进行封装
        List<ChapterVO> chapterVOList = new ArrayList<>();
        for(EduChapter e : chapterList){
            ChapterVO chapterVO = new ChapterVO();
            BeanUtils.copyProperties(e,chapterVO);

            //4、遍历查询小节list集合进行封装
            List<VideoVO> videoVOList = new ArrayList<>();
            for (int i = 0; i < videoList.size(); i++) {
                EduVideo eduVideo = videoList.get(i);
                if (eduVideo.getChapterId().equals(e.getId())){
                    VideoVO videoVO = new VideoVO();
                    BeanUtils.copyProperties(eduVideo,videoVO);
                    videoVOList.add(videoVO);
                }
            }
            chapterVO.setChildren(videoVOList);
            chapterVOList.add(chapterVO);
        }

        return chapterVOList;
    }

    //删除章节
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据章节id查小节
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("chapter_id",chapterId);
        int videoCount = videoService.count(videoQueryWrapper);
        if (videoCount > 0){
            throw new GuliException(20001,"不能删除");
        }else {
            int rows = baseMapper.deleteById(chapterId);
            return rows > 0;
        }
    }

    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }


}
